package com.tjsiqi.controller;

import com.tjsiqi.Vo.JbzInfoMVo;
import com.tjsiqi.common.RequestHolder;
import com.tjsiqi.model.SysUser;
import com.tjsiqi.service.SysUserService;
import com.tjsiqi.service.WebUserControlService;
import com.tjsiqi.util.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

/**
 * Created by lxc
 */

@Controller
public class UserController {

    @Resource
    private SysUserService sysUserService;

    @Resource
    WebUserControlService webUserControlService;
//  退出界面
    @RequestMapping("/logout.page")
    public void logout(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.getSession().invalidate();
        String path = "sign.jsp";
        response.sendRedirect(path);
    }
// 跳转首页面
    @RequestMapping("/index.page")
    public void index(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
//        request.getSession().invalidate();
        String path = "index.jsp";
        response.sendRedirect(path);
    }

//    @RequestMapping("/index.page")
//    public void index(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
////        request.getSession().invalidate();
//        String path = "index.jsp";
//        response.sendRedirect(path);
//    }
//  登录页面
    @RequestMapping("/login.page")
    @ResponseBody
    public void login(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        JbzInfoMVo jbzInfoMVo = null;
        SysUser sysUser = sysUserService.findByKeyword(username);
        // code 时间判断 不应为个人
        if(sysUser != null && sysUser.getJbzCode() != null) {
            jbzInfoMVo = webUserControlService.findByCode(sysUser.getJbzCode());
        }
        String errorMsg = "";
        String ret = request.getParameter("ret");
        String reti = request.getParameter("ptype");
        int ptype = Integer.valueOf(reti);
        if (StringUtils.isBlank(username)) {
            errorMsg = "用户名不可以为空";
        } else if (StringUtils.isBlank(password)) {
            errorMsg = "密码不可以为空";
        } else if (sysUser == null) {
            errorMsg = "查询不到指定的用户";
        } else if ( sysUser.getJbzCode() == null ) {
            errorMsg = "该用户未绑定搅拌站！";
        }else if (ptype == 1 && sysUser.getJbzCode().indexOf("0000")<0) {
            errorMsg = "您不是管理员，请选择用户登录";
        }else if (ptype == 0 && sysUser.getJbzCode().indexOf("0000")>0) {
            errorMsg = "您不是用户，请选择管理员登录";
        }else if (!sysUser.getPassword().equals(MD5Util.encrypt(password))) {
            errorMsg = "用户名或密码错误";
        } else if (sysUser.getStatus() != 1) {
            errorMsg = "账号已冻结，请联系所属搅拌站管理员开通";
        }  else if (ptype == 0 && jbzInfoMVo == null) {
            errorMsg = "查询不到该用户所属搅拌站，请刷新重试";
        }else if (ptype == 0 && jbzInfoMVo.getRemaintime().indexOf('-')>-1) {
            errorMsg = "账号已到期，请联系022-66351935开通";
        }else {
            // login success
            request.getSession().setAttribute("user", sysUser);
            if (StringUtils.isNotBlank(ret)) {
                response.sendRedirect(ret);
            } else {
                response.sendRedirect("/admin/index.page"); //TODO
                return;
            }
        }

        request.setAttribute("error", errorMsg);
        request.setAttribute("username", username);
        if (StringUtils.isNotBlank(ret)) {
            request.setAttribute("ret", ret);
        }
        String path = "sign.jsp";
        request.getRequestDispatcher(path).forward(request, response);
    }
}
